package finassist.view;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import finassist.controller.LoginUserManager;
import finassist.model.User;

import com.opensymphony.xwork2.ActionSupport;

/**
 * AJ Projects
 * UserLoginAction.java
 * Purpose: Action class for User Login Module. This class takes care of all login related actions.
 *
 * @author Karthick Natarajan
 * @version 1.0 9/10/13
 */

public class UserLoginAction extends ActionSupport implements SessionAware {

	private static final long serialVersionUID = 9149826260758390091L;
	private User user;
	private Long id;
	private Map session;
	private LoginUserManager linkController;

	public UserLoginAction() {
		linkController = new LoginUserManager();
	}

	public String execute() {
			return SUCCESS;
	}

	public String login() {
		user = getUser();
		System.out.println(getUser());
		int result = 0;

		try {
		result =	linkController.check(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(result ==1)
		return SUCCESS;
		else
		return ERROR;
	}

	public static List<Long> getKeysFromValue(Map<Long, String> hm, String value) {
		List<Long> list = new ArrayList<Long>();
		for (Long o : hm.keySet()) {
			if (hm.get(o).equals(value)) {
				list.add(o);
			}
		}
		return list;
	}

	public User getUser() {
		return user;
	}
	
	public void setUser(User user) {
		this.user = user;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public Map getSession() {
		return session;

	}
}
